2018-06-11
Sparse stuff:
Each processor has a specific set of instructions, called Instruction Set Architecture (ISA)
Bit: transistor/switch (on or off)
Assembly language
def. a low-level symbolic code converted by an assembler
ex.
> mov a1, 061h :
load in register a1 the content of memory location of adress 061h
easier to write but codes are extremely long and hard to read and understand
high-level languages
easier to write and read algorithms
> 1972: Dennis Ritchie at Bell telephone
Labs develops the C language
> 1979: Bjarne Stroustrup at Bell Labs
design and implement C++
(C with classes)
More sparse stuff:
compiler: g++
command line debugger: gdb
- not really practical to use
Integrated Development Environment (IDE)
##:- single line:
// - multiple lines:
/* ... */
- single line:
multiple file programs and header files
sparse stuff:
types
porto workshop week
operations for variable modification
prefix and postfix
implicit type conversion
ex.
conditional statement
pointer (dereference operator *)
a pointer is a variable that stores the address of another object
always initialize a pointer, *, with the value zero
pointers can be assigned addresses of other variables via the address-of operator (ampersand) & - the types of the variable and the pointer have to match
exs.
size
- the size of pointer doesn’t depend on its type
arrays
what Stroustroup says about:
declaration
indexes range from 0 to
size-1to access an element use the subscript operator
[]or through a pointer
boundary checking: C/C++ compiler does no boundary check. Using out of range indexes is not detected by compiler and results in computation errors
array list initialization
sizeof an array
returns the number of bytes allocated for the entire array
multidimensional arrays
for readability, use nested curly brackets
pointer arithmetic (with an ex)
- number of elements should be constant and could not be changed later
dynamic memory allocation
ex.
program memory
a programs memory is divided into
code area: compiled program (executable)
globals area: global variables
heap: dynamically allocated memory
stack: parameters and local variables
2018-07-02
strings
c++ 11 doesn’t allow string literals to be assigned to non-const char *
string concatenation
std::string // string class (data type)
string str = "Hello";
index range is 0 to
str.length()-1
common string methods
<cctype>: character manipulation
user-defined types
struct: sequence of elements (called members) of arbitrary typesunion: astructthat holds the value of just one of its elements at any one timeenum: a type with a set of named constants (called enumerators)
arrayaggregate data of the same type,structaggregate data of arbitrary type
structure
elements of a
structare called members or fieldsmember selection operator is the
.selecting a
structfield from a dereferenced pointer to thestructuses different operator:->
enumeration
exs.
2018-07-04
passing arguments by reference
What if I want to return more than one result with just one function?
Allow the function to modify the values of an argument: pass parameters by reference
exs.
static variable
ex.
2018-07-16
I/O stream library,
<iostream>, in order to display output to the standard output device (generally the screen) and get input from standard input device (generally the keyboard)
<fstream>extends<iostream>with additional features that make it possible to read from and write to (text and binary) files
A
streamcan be thought of as a sequence of bytes of infinite length that is used as a buffer to hold data that is waiting to be processed
declare a stream variable
open a file…
partial list of I/O manipulators
formatted stream input
extraction operation stops at white spaces
ex.
test if extraction/insertion has succeeded using the fail() method
operations for input streams
operations for output streams
2018-07-18
custom data types
a type is a concrete representation of a concept; e.g. float is a representation of real numbers
we design a new type to provide a definition of a concept that has no direct counterpart among the built-in types
the
classconstruct allows to define C++ classes, a tool for creating new types that can be used as conveniently similarly to the built-in types
classes main features
a
classis a user-defined type
member functions can define the meaning of initialization (creation), copy, move, and cleanup (destruction)
the public members provide the
class’s interface and theprivatemembers provide implementation details: encapsulation
a
structis aclasswhere members are by default public
by default, if you don’t specify, everything in the
classis private
ex.
the function implementation can be included in the class definition or outside. generally, code is textually separated into two files with the same name as the class / struct and different extensions
an implementation file .cpp for most member function implementations
a header file for the class / struct definition
access control
“user code”: code that uses the data type. “user” here doesn’t designate the application user
to restrict manipulation of the data members, use a
classinstead of astruct
in a
class, by default, the members are private i.e. accessible only by member functions
by restricting access to data members, a need for an initialization function arises e.g. Date::init
the private access specifier can be also used to emphasize the privacy of data members; both access specifiers can be used many times within the same class definition
advantages
any error/bug in manipulating the data members is now known to be in some member function => speeds up debugging
user (programmer) need only to examine the public interface (member function definitions) to learn how to use the data type
constructors
special member function executed when an object of a
classis instantiated
when a
classhas a constructor, all objects of thatclasswill be initialized by a constructor callconstructors typically used to initialize member variables to appropriate default values, or allow user to easily initialize member variables to his desired values
arguments with default values can also reduce the number of constructors needed
if the constructor requires arguments, they must be supplied
a constructor has the same name as the class and no return type (not
void)a
classcan have multiple constructors with respect to function overloadinginstantiating a
classthat doesn`t have a constructor means that the members of the resulting object will be uninitialized
ex.
destructors
special member function called when an object goes out of scope or explicitly destroyed
used to free dynamically allocated memory or do some cleanup like closing files, release a lock, …
the destructor must have the same name as the class, preceded by a tilde (~)
the destructor can`t take arguments
the destructor has no return type
only one destructor per class
ex.
shallow vs. deep copy
if a member is a pointer and the source object member points to dynamically allocated object or array, then the copy member will get the same address thus pointing to the same dynamically allocated object or array
if this is not the wanted behavior, you need to define the copy behavior via a copy constructor and an assignment operator
ex.
operator overloading
declaring and implementing functions to define the behavior of operators for some specific data type
for example, the addition operator+:
has been overloaded to do concatenation for strings
can be overloaded to do complex number arithmetic for a
classrepresenting complex numbers
functions have names in the format “operator@” where @ is one of the following operators:
exs.
“friend” functions are not member functions yet they have access to private members
if operands not of the same type and operator is commutative then need to implement two versions of operator overload with different parameter order
need to be careful with return type: “
this” returns a reference to aclassobject
increment and decrement operators
the increment and decrement operators modify the object they are called on
they have different behaviors when used in prefix or in postfix
need a different implementation of operator++ or operator– for prefix and postfix usage
to overload the function twice for the same type, one needs a different function signature => a dummy int argument is allowed for implementing postfix version
prefix: the modification has to be reflected in the object before it is used in an expression
- the prefix implementation requires returning the object by reference for the modification to take effect in the expression evaluation
postfix: the modification is reflected only after expression is evaluated
- the prefix implementation needs to modify the object but return a copy of the old value to be used in the expression
ex.
overloading I/O operators
overloading assignment operator and the copy constructor
assignment overload function must be a member function
copy constructor must take a parameter of the same type passed by reference or const reference but NOT by value
- this allows to check for self assignment
assignment operator vs. copy constructor:
same behavior: copy data from one object to the other
difference: assignment is used when first operand already exist (previously instantiated)
for more operator overloading exs, see
Sys.time()## [1] "2018-07-19 09:38:32 +03"